/**
 * @author Scott Koon
 */
twitterratio.data = {
    getFriends: function(user) {
        var friendsUrl = "https://twitter.com/statuses/friends.json";
		var success = function() {};
		var error =  function(xmlRequest, textStatus, errorThrown) {
				var error = new twitterratio.error("Could not get friends", textStatus);
				twitterratio.onError(error);
			};
		twitterratio.data.makeAPICall(friendsUrl,null,success, error);
    },
    getFollowers: function(user) {
        var followersUrl = "https://twitter.com/statuses/followers.json";
		var success = function() {};
		var error =  function(xmlRequest, textStatus, errorThrown) {
				var error = new twitterratio.error("Could not get followers", textStatus);
				twitterratio.onError(error);
			};
		twitterratio.data.makeAPICall(followersUrl,null,success, error);		
    },
    followUser: function(userId) {
        var followUserUrl = "https://twitter.com/friendships/create/" + userId + ".json";
		var success = function() {};
		var error =  function(xmlRequest, textStatus, errorThrown) {
				var error = new twitterratio.error("Could not follow user", textStatus);
				twitterratio.onError(error);
			};
		twitterratio.data.makeAPICall(followUserUrl,null,success, error);
    },
    unfollowUser: function(userId) {
        var unfollowUrl = "https://twitter.com/friendships/destroy/" + userId + ".json";
   		var success = function() {};
		var error = function(xmlRequest, textStatus, errorThrown){
			var error = new twitterratio.error("Could not unfollow user", textStatus);
			twitterratio.onError(error);
		};
		twitterratio.data.makeAPICall(unfollowUrl,null,success, error);
	},
    blockUser: function(user) {
        var blockUserUrl = "";
		throw "Not implemented";
    },
	makeAPICall : function(urlToCall, dataToSend, successCallback, errorCallback) {
		$.ajaxSetup({
			username: twitterratio.currentUser.username,
			password: twitterratio.currentUser.password,
			type: "POST",
			beforeSend: function(xmlRequest){
				xmlRequest.setRequestHeader("Authorization", "Basic " + Base64.encode(twitterratio.currentUser.username + ":" + twitterratio.currentUser.password));
				xmlRequest.setRequestHeader("Cookie", "");
			},
			error : errorCallback,
			success : successCallback
		});
		$.ajax( {
			url: urlToCall,
			data : dataToSend
		});		
	},
	validateLogin: function(username, password) {
		if(!username.isEmpty() && !password.isEmpty()) {
			try {
				var validateUrl = "https://twitter.com/account/verify_credentials.json";
				var success = function(data, textStatus) {
					alert(data);
				};
				var error = function(xmlRequest, textStatus, errorThrown) {
					var error = new twitterratio.error("Could not validate password", textStatus);
					twitterratio.onError(error);
				};
				twitterratio.data.makeAPICall(validateUrl, success, error);
			} catch(ex) {
				var err = new twitterratio.error(ex);
				twitterratio.onError(err);
			};
			var getUserUrl = "http://twitter.com/users/show/" + username + ".json";
			
			twitterratio.currentUser = new twitterratio.user(username, password);
		};
	}
};
